# default configure
from Shell.configure.ConfigureAccess import ConfigureGet
from Shell.model import GameLobbyServer, GameLobbyClient
from GameLobby import GameLobbyView, GameLobbyController
# event listener
from direct.showbase import DirectObject
# client start
from Shell.fsm.EventDefine import event_NETWORKGAME_GAMELOBBY_CLIENTCREATE

class GameLobbyFrame(DirectObject.DirectObject):
    def __init__(self):
        # get current login configure
        ConnectType = ConfigureGet("Login")["ConnectType"]
        if ConnectType == "server":
            self.model = GameLobbyServer()
            self.view = GameLobbyView(self.model)
            self.controller = GameLobbyController(self.model, self.view)
        else:
            self.model = GameLobbyClient()
            # client: create game lobby gui after map loading
            self.acceptOnce(event_NETWORKGAME_GAMELOBBY_CLIENTCREATE, self.ClientCreateGameLobbyHandle)

    def ClientCreateGameLobbyHandle(self):
        self.view = GameLobbyView(self.model)
        self.controller = GameLobbyController(self.model, self.view)

    def destroy(self, bReleaseMap=True):
        self.ignoreAll()
        # client may not create controller and view
        if hasattr(self, "controller"):
            self.controller.destroy()
            self.view.destroy()
        self.model.destroy(bReleaseMap)
